//
//  MessageAlertViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MessageAlertViewController.h"
#import "BusinessService+User.h"
#import "SVProgressHUD.h"
#import "AppDelegate.h"
@interface MessageAlertViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation MessageAlertViewController
{
    int msgset1;
    int msgset2;
    int msgset3;
    int msgset4;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
}
-(void)viewWillAppear:(BOOL)animated{
    [[AppDelegate shareDelegate]refdata:^(id res) {
        NSString *msg_setting = [AppDelegate shareDelegate].user.msg_setting;
        msgset1 = [[msg_setting substringWithRange:NSMakeRange(0,1)] intValue];
        msgset2 = [[msg_setting substringWithRange:NSMakeRange(1,1)] intValue];
        msgset3 = [[msg_setting substringWithRange:NSMakeRange(2,1)] intValue];
        msgset4 = [[msg_setting substringWithRange:NSMakeRange(3,1)] intValue];
        [_table reloadData];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
//        return 4;
        return 3; //xhw2018-2-9加
    }
    if (section == 1) {
        return 2;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"新消息提醒";
    }
    if (section == 1) {
        return @"新消息提醒";
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.textLabel.text = @"接收新消息提醒";
            UISwitch *switchs = [[UISwitch alloc]initWithFrame:CGRectMake(SCRE_WIDTH-70, 5, 90, 30)];
            switchs.tag = 100;
            [switchs addTarget:self action:@selector(switchsAction:) forControlEvents:UIControlEventValueChanged];
            if (msgset1==0) {
                [switchs setOn:NO];
            }else{
                [switchs setOn:YES];
            }
            [cell.contentView addSubview:switchs];
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"声音";
            UISwitch *switchs = [[UISwitch alloc]initWithFrame:CGRectMake(SCRE_WIDTH-70, 5, 90, 30)];
            switchs.tag = 101;
            [switchs addTarget:self action:@selector(switchsAction:) forControlEvents:UIControlEventValueChanged];
            if (msgset2==0) {
                [switchs setOn:NO];
            }else{
                [switchs setOn:YES];
            }
            [cell.contentView addSubview:switchs];
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"震动";
            UISwitch *switchs = [[UISwitch alloc]initWithFrame:CGRectMake(SCRE_WIDTH-70, 5, 90, 30)];
            switchs.tag = 102;
            [switchs addTarget:self action:@selector(switchsAction:) forControlEvents:UIControlEventValueChanged];
            if (msgset3==0) {
                [switchs setOn:NO];
            }else{
                [switchs setOn:YES];
            }
            [cell.contentView addSubview:switchs];
        }
        if (indexPath.row == 3) {
            cell.textLabel.text = @"提醒时显示消息内容";
            UISwitch *switchs = [[UISwitch alloc]initWithFrame:CGRectMake(SCRE_WIDTH-70, 5, 90, 30)];
            switchs.tag = 103;
            [switchs addTarget:self action:@selector(switchsAction:) forControlEvents:UIControlEventValueChanged];
            if (msgset4==0) {
                [switchs setOn:NO];
            }else{
                [switchs setOn:YES];
            }
            [cell.contentView addSubview:switchs];
        }
    }
    if(indexPath.section == 1){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 0) {
            cell.textLabel.text = @"勿扰时段";
            if([[AppDelegate shareDelegate].user.wurao_start trim].length !=0){
                cell.detailTextLabel.text = @"已开启";
            }else{
                cell.detailTextLabel.text = @"关闭";
            }
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"功能提醒";
//            cell.detailTextLabel.text = @"关闭";
        }
    }
    return cell;
}
#pragma mark 设置消息提示
-(void)switchsAction:(UISwitch *)sw{
    if(sw.tag == 100){//新消息提醒
        if (!sw.on) {
            msgset1 = 0;
        }else{
            msgset1 = 1;
        }
    }
    if(sw.tag == 101){//声音
        if (!sw.on) {
            msgset2 = 0;
        }else{
            msgset2 = 1;
        }
    }
    if(sw.tag == 102){//震动
        if (!sw.on) {
            msgset3 = 0;
        }else{
            msgset3 = 1;
        }
    }
    if(sw.tag == 103){//消息提醒显示消息内容
        if (!sw.on) {
            msgset4 = 0;
        }else{
            msgset4 = 1;
        }
    }
    NSString *msg_setting = [NSString stringWithFormat:@"%d%d%d%d",msgset1,msgset2,msgset3,msgset4];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:msg_setting forKey:@"msg_setting"];
    [[BusinessService shareInstance]msg_setting:dic success:^(id result) {
        
        //xhw2018-2-9加
        [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
            [AppDelegate shareDelegate].user = result;
            [[AppDelegate shareDelegate] xhwcanPlaySound];
        } failure:^(id result) {
            NSLog(@"失败");
        }]; //xhw2018-2-9加
        
        [SVProgressHUD showSuccessWithStatus:@"设置成功"];
        [_table reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DisturbVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        if (indexPath.row == 1) {
            
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FunctionVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
        }
    }
    
}





- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
